package com.kupurui.asteacher.ui.job;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.android.frame.util.AppJsonUtil;
import com.android.frame.util.Toolkit;
import com.android.frame.view.linearlistview.LinearListView;
import com.kupurui.asteacher.R;
import com.kupurui.asteacher.adapter.SubmitDetailsAdapter1;
import com.kupurui.asteacher.adapter.SubmitDetailsAdapter2;
import com.kupurui.asteacher.adapter.SubmitDetailsAdapter3;
import com.kupurui.asteacher.bean.SubmitDetailsInfo;
import com.kupurui.asteacher.http.Teacher;
import com.kupurui.asteacher.ui.BaseAty;
import com.kupurui.asteacher.utils.UserManger;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 提交详情
 * Created by Administrator on 2017/7/25.
 */

public class SubmitDatailsAty extends BaseAty {
    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.radiobtn_all)
    RadioButton radiobtnAll;
    @Bind(R.id.radiobtn_submit)
    RadioButton radiobtnSubmit;
    @Bind(R.id.radiobtn_remark)
    RadioButton radiobtnRemark;
    @Bind(R.id.radiobtn_unsubmit)
    RadioButton radiobtnUnsubmit;
    @Bind(R.id.listview1)
    LinearListView listview1;
    @Bind(R.id.listview2)
    LinearListView listview2;
    @Bind(R.id.listview3)
    LinearListView listview3;
    @Bind(R.id.tv_submit)
    TextView tvSubmit;
    @Bind(R.id.tv_unsubmit)
    TextView tvUmsubmit;
    @Bind(R.id.tv_comment)
    TextView tvComment;


    SubmitDetailsInfo submitDetailsInfo;
    List<SubmitDetailsInfo.TijiaoBean> submitList;//已提交
    List<SubmitDetailsInfo.WeitijiaoBean> unSubmitList;//未提交
    List<SubmitDetailsInfo.PingjiaBean> commentList;//已评

    SubmitDetailsAdapter1 adapter1;
    SubmitDetailsAdapter2 adapter2;
    SubmitDetailsAdapter3 adapter3;


    private String work_id = "";//作业ID
    private String class_id = "";//班级ID

    @Override
    public int getLayoutId() {
        return R.layout.submit_details_aty;
    }

    @Override
    public void initData() {
        if (getIntent().getExtras() != null) {
            work_id = getIntent().getStringExtra("id");
            class_id = getIntent().getStringExtra("class_id");
        }


        submitList = new ArrayList<>();
        unSubmitList = new ArrayList<>();
        commentList = new ArrayList<>();

        adapter1 = new SubmitDetailsAdapter1(this, submitList, R.layout.submit_details_item);
        listview1.setAdapter(adapter1);

        adapter2 = new SubmitDetailsAdapter2(this, commentList, R.layout.submit_details_item);
        listview2.setAdapter(adapter2);

        adapter3 = new SubmitDetailsAdapter3(this, unSubmitList, R.layout.submit_details_item);
        listview3.setAdapter(adapter3);

        listview1.setOnItemClickListener(new LinearListView.OnItemClickListener() {
            @Override
            public void onItemClick(LinearListView parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putString("id",submitList.get(position).getWork_id());
                bundle.putString("st_id",submitList.get(position).getSid());
                bundle.putString("name",submitList.get(position).getUsername());
                bundle.putString("class_id",class_id);
                startActivity(JobStudentDetailsAty.class, bundle);
            }
        });

        listview2.setOnItemClickListener(new LinearListView.OnItemClickListener() {
            @Override
            public void onItemClick(LinearListView parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putString("id",commentList.get(position).getWork_id());
                bundle.putString("st_id",commentList.get(position).getSid());
                bundle.putString("name",commentList.get(position).getUsername());
                bundle.putString("class_id",class_id);
                startActivity(JobStudentDetailsAty.class, bundle);
            }
        });

        listview3.setOnItemClickListener(new LinearListView.OnItemClickListener() {
            @Override
            public void onItemClick(LinearListView parent, View view, int position, long id) {
                //催作业
                showLoadingDialog("");
                new Teacher().cui_work(UserManger.getId(),unSubmitList.get(position).getSt_id(),work_id,SubmitDatailsAty.this,1);
            }
        });

    }

    @Override
    public void requestData() {
        showLoadingContent();
        new Teacher().tijiaoDetail(work_id,class_id,this,0);
    }

    @OnClick({R.id.iv_back,R.id.radiobtn_all,R.id.radiobtn_submit,R.id.radiobtn_remark,R.id.radiobtn_unsubmit})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.radiobtn_all://全部
                listview1.setVisibility(View.VISIBLE);
                listview2.setVisibility(View.VISIBLE);
                listview3.setVisibility(View.VISIBLE);
                showText();
                break;
            case R.id.radiobtn_submit://已提交
                listview1.setVisibility(View.VISIBLE);
                listview2.setVisibility(View.GONE);
                listview3.setVisibility(View.GONE);
                hideText();
                break;
            case R.id.radiobtn_remark://已填评语
                listview1.setVisibility(View.GONE);
                listview2.setVisibility(View.VISIBLE);
                listview3.setVisibility(View.GONE);
                hideText();
                break;
            case R.id.radiobtn_unsubmit://未提交
                listview1.setVisibility(View.GONE);
                listview2.setVisibility(View.GONE);
                listview3.setVisibility(View.VISIBLE);
                hideText();
                break;
        }
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        switch (what){
            case 0:
                submitDetailsInfo = AppJsonUtil.getObject(result,SubmitDetailsInfo.class);
                submitList.clear();
                commentList.clear();
                unSubmitList.clear();
                submitList.addAll(submitDetailsInfo.getTijiao());
                adapter1.notifyDataSetChanged();
                commentList.addAll(submitDetailsInfo.getPingjia());
                adapter2.notifyDataSetChanged();
                unSubmitList.addAll(submitDetailsInfo.getWeitijiao());
                adapter3.notifyDataSetChanged();
                showText();
                break;
            case 1:
                showToast(AppJsonUtil.getResultInfo(result).getMessage());
                break;
        }
        super.onSuccess(result, call, response, what);
    }

    private void hideText(){
        tvSubmit.setVisibility(View.GONE);
        tvUmsubmit.setVisibility(View.GONE);
        tvComment.setVisibility(View.GONE);
    }

    private void showText(){
        if (Toolkit.listIsEmpty(submitList)){
            tvSubmit.setVisibility(View.GONE);
        }else {
            tvSubmit.setVisibility(View.VISIBLE);
        }
        if (Toolkit.listIsEmpty(unSubmitList)){
            tvUmsubmit.setVisibility(View.GONE);
        }else{
            tvUmsubmit.setVisibility(View.VISIBLE);
        }

        if (Toolkit.listIsEmpty(commentList)){
            tvComment.setVisibility(View.GONE);
        }else{
            tvComment.setVisibility(View.VISIBLE);
        }

    }

}
